home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-28 | 5.6 KB | 259 lines | [TEXT/MPS ] |
- /*
- File: SlotThread.cp
-
- Contains: xxx put contents here xxx
-
- Written by: Tim Harnett
-
- Copyright: © 1994 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <17> 2/14/95 TMH replace GetEWorldSlot and GetHFSSlot with GetExternalSlot
- <16> 2/14/95 TMH added IsLetterInTheEventQueue
- <15> 2/10/95 TMH change wireless to HFS
- <14> 2/7/95 TMH stop sending if disconnect recieved
- <13> 2/6/95 TMH implemented bRecieveAfterSendNow
- <12> 1/13/95 TMH removed EWorldSlot and HFS slot dependency
- <11> 1/11/95 TMH tweak to generalize framework
- <10> 12/16/94 TMH avoid sending (immediate) letter twice
- <9> 12/12/94 TMH use of thread events
- <8> 11/17/94 TMH connect only once
- <7> 11/12/94 TMH removed reference to CommThread
- <6> 11/8/94 TMH added fIdentifierTag argument
- <5> 10/11/94 TMH CommThread integration
- <4> 10/6/94 TMH fixed bug in DoOutgoing
- <3> 10/3/94 TMH break of THFS&TEWorld from TExternalSlot
- <2> 9/30/94 TMH added DoIncoming
- <1> 9/21/94 TMH seperated from Application.cp
- 9/21/94 TMH xxx put comment here xxx
-
- To Do:
- */
-
- #ifndef __SlotThread__
- #include "SlotThread.h"
- #endif
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __Debug__
- #include "Debug.h"
- #endif
-
- #ifndef __Application__
- #include "Application.h"
- #endif
-
- #ifndef __Globals__
- #include "Globals.h"
- #endif
-
- #ifndef __MSAMSlot__
- #include "MSAMSlot.h"
- #endif
-
- #ifndef __ExternalSlot__
- #include "ExternalSlot.h"
- #endif
-
- #ifndef __ALetter__
- #include "ALetter.h"
- #endif
-
- #ifndef __LogErrors__
- #include "LogErrors.h"
- #endif
-
-
- Boolean gRecieveAfterSendNow = false;
-
-
- //-----------------------------------------------
- // T S l o t T h r e a d
- //-----------------------------------------------
-
-
-
- //-------------------------------------------------------------------------------------
- TSlotThread::TSlotThread()
- {
- fMSAMSlot = 0;
- }
-
-
-
- //-------------------------------------------------------------------------------------
- void TSlotThread::ISlotThread(TMSAMSlot* slot)
- {
- ASSERT(slot);
- fMSAMSlot = slot;
-
- this->ICooperativeThread('slot',kDefaultThreadStackSize,kNewSuspend+kNeedEventQueue);
-
-
- }
-
- #pragma segment SlotThread
- //-------------------------------------------------------------------------------------
- void* TSlotThread::ThreadMain()
- {
-
- while( true ) {
-
- FailInfo fi;
- Try(fi) {
-
-
- // Events are handle one at time. If we are processing all outgoing
- // and incoming all are done before we are back here checking for other
- // events. Whenever we are waiting for an event assume that we are disconnected.
- //••• deal with prioritized events
-
-
- CThreadEvent* event = this->WaitNextEvent();
-
- TExternalSlot* xSlot = fMSAMSlot->GetExternalSlot();
-
- ASSERT( xSlot );
- if( xSlot == 0 )
- continue;
-
-
-
- switch( event->EventID() ) {
-
- case kMailEPPCContinue:
- case kMailEPPCModifySlot:
- xSlot->SetConfiguration();
- // intentionally fall thru see if letters should be sent
- case kMailEPPCMsgPending:
-
- if( !xSlot->ShouldSendWaitingLetters() )
- break;
-
- // fall thru
-
- case kMailEPPCSchedule:
-
- xSlot->Connect();
-
- xSlot->SendOutgoingLetters();
-
- if( !this->HasUserInterrupted() )
- xSlot->ReceiveIncomingLetters();
-
- xSlot->Disconnect();
-
- if( this->HasUserInterrupted() )
- this->ClearUserInterrupt();
-
- break;
-
-
- case kMailEPPCSendImmediate:
- {
-
- Boolean connected = false;
- Boolean moreSendImmediates = false;
-
- do {
-
- long letterSeqNo = event->EventData0();
- if( !fMSAMSlot->IsLetterDone(letterSeqNo) ) { // in case the letter has already been sent.
-
- if( !connected ) { // do this only for the first letter we send.
- xSlot->Connect();
- connected = true;
- }
-
-
- TOutgoingALetter* outgoingLetter = new TOutgoingALetter;
- outgoingLetter->IOutgoingALetter(fMSAMSlot,letterSeqNo);
- xSlot->SendLetter(outgoingLetter);
-
-
- }
-
- // Check the event queue for more letters to send now. We make no attempt
- // to process events other than kMailEPPCSendImmediate. If there are no kMailEPPCSendImmediate.
- // we disconnect. And process other events normally. With greater effort
- // a more intelligent approach could be implemented. But not today as this
- // is simple and makes sense most of the time.
-
-
- if( this->HasUserInterrupted() )
- break;
-
-
- moreSendImmediates = false;
- event = this->EventAvail(); // peek into the event queue
- if( (event != 0) && (event->EventID() == kMailEPPCSendImmediate) ) {
- event = this->GetNextEvent();
- moreSendImmediates = true;
- }
-
-
- } while( moreSendImmediates );
-
-
-
- if( gRecieveAfterSendNow && !this->HasUserInterrupted() )
- xSlot->ReceiveIncomingLetters();
-
-
- xSlot->Disconnect();
-
- if( this->HasUserInterrupted() )
- this->ClearUserInterrupt();
-
-
- }
- break;
-
- default:
- ASSERTPRINT(false,("Unknown slot event.\n"));
- break;
- };
-
-
- fi.Success();
- } else {
-
- LogError(fi.error,fMSAMSlot);
-
- }
-
-
-
- }
-
- return this;
- }
-
-
- //-----------------------------------------------------------------------------------------------------------------------
- Boolean TSlotThread::IsLetterInTheEventQueue(long letterSeqNo,Boolean deleteIt)
- {
- Boolean isInQueue = false;
-
- CThreadEventIterator iter(this);
- for(CThreadEvent* threadEvent = iter.FirstEvent(); iter.More(); threadEvent = iter.NextEvent() ) {
-
- if( threadEvent->EventData0() == letterSeqNo ) {
-
- if( deleteIt )
- iter.FlushCurrentEvent();
-
- isInQueue = true;
- break;
- }
-
- }
-
- return isInQueue;
-
- }
-